// Component/navbar/index.js
const app = getApp();
Component({
  properties: {
    background: {
      type: String,
      value: 'rgba(255, 255, 255, 1)'
    },

    color: {
      type: String,
      value: '#00000'
    },

    titlecontent:{
      type: String,
      value: ''
    },

    titleText: {
      type: String,
      value: ''
    },
    titleImg: {
      type: String,
      value: ''
    },
    backIcon: {
      type: String,
      value: ''
    },
    homeIcon: {
      type: String,
      value: ''
    },
    fontSize: {
      type: Number,
      value: 36
    },
    iconHeight: {
      type: Number,
      value: 40
    },
    iconWidth: {
      type: Number,
      value: 40
    },
    titleLists:{
      type: [],
      value:undefined
    },
    onclass:{
      type:Object,
      value:undefined
    }

  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached: function () {
      var that = this;
      that.setNavSize();
      that.setStyle();
    },
    moved: function () {},
    detached: function () {},
  },

  data: {
    navwidth:'',
    navheight:'',
    titleListsindex:0,
    homepage:"/pages/home/index",
  },
  methods: {
    // 通过获取系统信息计算导航栏高度 
    setNavSize: function () {
      var that = this,
        sysinfo = wx.getSystemInfoSync(),
        statusHeight = sysinfo.statusBarHeight,
        isiOS = sysinfo.system.indexOf('iOS') > -1,
        navHeight;
        let menuButtonObject = wx.getMenuButtonBoundingClientRect();
        wx.getSystemInfo({
          success: res => {
            let statusBarHeight = res.statusBarHeight,
              navTop = menuButtonObject.top,//胶囊按钮与顶部的距离
              navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight)*2;//导航高度
           that.setData({
            navwidth:menuButtonObject.width,
            navheight:menuButtonObject.height
           });
          },
          fail(err) {
            console.log(err);
          }
        })
      if (!isiOS) {
        navHeight = 48;
      } else {
        navHeight = 44;
      }
      that.setData({
        status: statusHeight,
        navHeight: navHeight
      })
      app.globalData.navheight =    statusHeight + navHeight;
      app.globalData.statusHeight= statusHeight;
    },
    setStyle: function () {
      var that = this,
        containerStyle, textStyle, iconStyle;
      containerStyle = [
        'background:' + that.data.background
      ].join(';');
      textStyle = [
        'color:' + that.data.color,
        'font-size:' + that.data.fontSize + 'rpx'
      ].join(';');
      iconStyle = [
        'width: ' + that.data.iconWidth + 'px',
        'height: ' + that.data.iconHeight + 'px'
      ].join(';');
      that.setData({
        containerStyle: containerStyle,
        textStyle: textStyle,
        iconStyle: iconStyle
      })
    },
    // 返回事件 
    back: function () {
      var pages =getCurrentPages();
      console.log(pages);
      var prevpage = pages[pages.length-1];
      console.log(prevpage);
      if(prevpage==undefined ||  pages.length==1){
          wx.switchTab({
            url: this.data.homepage,
          })
      }
      else{
      wx.navigateBack({
        delta: 1
      })
     }
      this.triggerEvent('back', {
        back: 1
      })
    
    },
    home: function () {
      console.log("/pages/home/index");
      wx.switchTab({
        url: this.data.homepage
    });
      this.triggerEvent('home', {});
    },

    navoperation(e){
        var titlelist = e.currentTarget.dataset.item;
        
        var index= e.currentTarget.dataset.index;
         titlelist = this.data.titleLists[index];
         console.log(titlelist);
        if(titlelist!= undefined && titlelist.func!=undefined){
          console.log(titlelist);
          titlelist.func();
        }

        this.setData({
          titleListsindex:index
        });
    },

    onclass(){
      var onclass= this.data.onclass;
      if(onclass!= undefined && onclass.func!=undefined){
        console.log(onclass);
        onclass.func();
      }
    }

  }
})